package com.yx.front.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.front.config.global.BizException;
import com.yx.front.config.global.CommonResult;
import com.yx.front.model.ChapterMessage;
import com.yx.front.model.param.ChapterMessageParam;
import com.yx.front.model.vo.BookChapterVo;
import com.yx.front.model.vo.ChapterMessageVo;
import com.yx.front.service.ChapterMessageService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.yx.front.converter.ChapterMessageConverter.getChapterMessageVo;
import static com.yx.front.converter.ChapterMessageConverter.handlerPage;

/**
 * @author YangXiong
 */
@Component
public class ChapterMessageManager {

    @Resource
    private ChapterMessageService chapterMessageService;

    public CommonResult<List<BookChapterVo>> getPage(ChapterMessageParam param) {
        if (Objects.isNull(param.getBookId())) {
            throw new BizException("参数不能为空，请稍后再试");
        }
        LambdaQueryWrapper<ChapterMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChapterMessage::getBookId, param.getBookId());
        Page<ChapterMessage> chapterMessagePage = new Page<>();
        chapterMessagePage.setCurrent(param.getCurrentPage());
        chapterMessagePage.setSize(param.getTotalPage());
        Page<ChapterMessage> page = chapterMessageService.page(chapterMessagePage, queryWrapper);
        List<BookChapterVo> result = handlerPage(page.getRecords());
        return CommonResult.success(result, page.getTotal());
    }

    public CommonResult<ChapterMessageVo> getDetailById(ChapterMessageParam param) {
        if(Objects.isNull(param)){
            throw new BizException("参数不能为空");
        }
        ChapterMessage chapterMessage = chapterMessageService.getById(param.getId());
        System.out.println(chapterMessage);
        ChapterMessageVo result = getChapterMessageVo(chapterMessage);
        return CommonResult.success(result);
    }

    public CommonResult<List<Long>> getList(ChapterMessageParam param) {
        if (Objects.isNull(param.getBookId())) {
            throw new BizException("参数不能为空，请稍后再试");
        }
        LambdaQueryWrapper<ChapterMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChapterMessage::getBookId, param.getBookId());
        Page<ChapterMessage> chapterMessagePage = new Page<>();
        chapterMessagePage.setCurrent(param.getCurrentPage());
        chapterMessagePage.setSize(3000);
        List<ChapterMessage> records = chapterMessageService.page(chapterMessagePage, queryWrapper).getRecords();
        Set<Long> set = records.stream().collect(Collectors.toMap(ChapterMessage::getId, Function.identity())).keySet();
        ArrayList<Long> list = new ArrayList<>(set);
        Collections.sort(list);
        return CommonResult.success(list);
    }
}
